//
//  LookAllViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/20.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "LookAllViewController.h"
#import "ShopDetailViewController.h"
#import "ShopCollectionCell.h"
#define KColumCount 3
#define spaceWith 10

@interface LookAllViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionV;
@property(nonatomic,strong)NSMutableArray *dataSouce;

@end

@implementation LookAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSouce = [NSMutableArray array];
    //创建collectionView
    [self initCollectionV];
    //回到顶部按钮
    [self backToTop];
    //设置需要显示自定义naviGationBar的右侧信封
    self.isNeedMessageItem =YES;
    [self loadData];
}

-(void)loadData{
   // 2.7根据商场类别查询全部商场接口
     [MBProgressHUD showLoadingGif];
    NSDictionary *paraDic = @{
                              @"markettypeid":self.markettypeid
                              };
         __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:MARKETSHOWALLMARKET paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"根据商场类别查询全部商场接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *dataArr = resultDic[@"data"];
            if([dataArr isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismissHUD];
                return ;
            }
            for(NSDictionary *dic in dataArr){
                ShopCellModel *model = [[ShopCellModel alloc]init];
                model.marketId = dic[@"marketId"];
                model.marketPics = dic[@"mPic"];
                model.marketName = dic[@"marketname"];
                [weakSelf.dataSouce addObject:model];
            }
            if(!weakSelf.dataSouce.count){
                 [MBProgressHUD dismissHUD];
                [weakSelf.view addSubview:weakSelf.noDataIUmageV];
                return;
            }else{
                [weakSelf.noDataIUmageV removeFromSuperview];
            }
            [weakSelf.collectionV reloadData];
            weakSelf.collectionV.hidden = NO;
            [MBProgressHUD dismissHUD];
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

- (void)initCollectionV{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
      //设置每一个item之间的空隙
    layout.minimumInteritemSpacing = 10;
    //设置每一行之间的距离
    layout.minimumLineSpacing = 20;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width,Main_Screen_Height-kNaviBarHeight) collectionViewLayout:layout];
    _collectionV = collectionV;
    //设置分页
//    collectionV.pagingEnabled = YES;
    collectionV.showsHorizontalScrollIndicator = NO;
    collectionV.showsVerticalScrollIndicator = NO;
    [self.view  addSubview:collectionV];
    collectionV.backgroundColor = [UIColor whiteColor];
    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"ShopCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ShopCollectionCell"];
    self.collectionV = collectionV;
    collectionV.hidden = YES;
}

-(void)backToTop{
    UIButton *toTopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    toTopButton.frame = CGRectMake(Main_Screen_Width-52, Main_Screen_Height-60, 42, 42);
    [toTopButton setImage:[UIImage imageNamed:@"button_zhiding_nor"] forState:UIControlStateNormal];
    [toTopButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:toTopButton];
    [self.view bringSubviewToFront:toTopButton];
}

-(void)BackAction{
    [_collectionV scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0]
                           atScrollPosition:UICollectionViewScrollPositionNone
                                   animated:YES];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.dataSouce.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    ShopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopCollectionCell" forIndexPath:indexPath];
    ShopCellModel *model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    return CGSizeMake((Main_Screen_Width-4*spaceWith)/3, 145);
    CGFloat with = (Main_Screen_Width-4*10-2*18)/3;
    CGFloat height = with*1.45;
    return CGSizeMake(with, height);

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets edge = UIEdgeInsetsMake(20, 10, 0 ,10);
    return edge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ShopCellModel *model = self.dataSouce[indexPath.row];
    ShopDetailViewController  *shopDetail = [[ShopDetailViewController alloc]init];
    shopDetail.vcName = model.marketName;
    shopDetail.marketId =model.marketId;
    shopDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:shopDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
